<template>
  <el-menu
    active-text-color="#ffd04b"
    background-color="#545c64"
    class="el-menu-vertical-demo"
    :default-active="selectIndex"
    text-color="#fff"
    @open="handleOpen"
    @close="handleClose"
  >
    <el-menu-item index="0" @click="toLink('main')">
      <el-icon><house /></el-icon>
      <span slot="title">首页</span>
    </el-menu-item>

    <el-sub-menu index="1">
      <template #title>
        <el-icon><operation /></el-icon>
        <span>系统管理</span>
      </template>
      <el-menu-item-group>
        <el-menu-item index="1-1" @click="toLink('user')">
          <el-icon><user /></el-icon>
          <span slot="title">用户管理</span>
        </el-menu-item>
        <el-menu-item index="1-2" @click="toLink('role')">
          <el-icon><place /></el-icon>
          <span slot="title">角色管理</span>
        </el-menu-item>
        <el-menu-item index="1-3" @click="toLink('menu')">
          <el-icon><grid /></el-icon>
          <span slot="title">菜单管理</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-sub-menu>

    <el-sub-menu index="2">
      <template #title>
        <el-icon><tools /></el-icon>
        <span>系统工具</span>
      </template>

      <el-menu-item-group @click="toLink('book')">
        <el-menu-item index="2-1">
          <el-icon><notebook /></el-icon>
          <span slot="title">数字字典</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-sub-menu>
  </el-menu>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import emitter from "../plugins/evenHub";
export default defineComponent({
  name: "AsideMenu",
  setup() {
    const router = useRouter();
    const selectIndex = ref("0")

    const handleOpen = (key: string, keyPath: string[]) => {
      console.log(key, keyPath);
    };
    const handleClose = (key: string, keyPath: string[]) => {
      console.log(key, keyPath);
    };
    const toLink = (routerName: any) => {
      if (routerName === "main") {
        emitter.emit("addTable", {
          title: "首页",
          name: "1",
        });
      }
      if (routerName === "user") {
        emitter.emit("addTable", {
          title: "用户管理",
          name: "2",
        });
      }
      if (routerName === "role") {
        emitter.emit("addTable", {
          title: "角色管理",
          name: "3",
        });
      }
      if (routerName === "menu") {
        emitter.emit("addTable", {
          title: "菜单管理",
          name: "4",
        });
      }
      if (routerName === "book") {
        emitter.emit("addTable", {
          title: "数字字典",
          name: "5",
        });
      }

      router.push({ name: routerName });
    };

    const chengSelect = (indexValue:any)=>{
      selectIndex.value = indexValue
    }
    onMounted(() => {
      emitter.on("chengSelect", chengSelect);
    
    });

    return {
      handleOpen,
      handleClose,
      toLink,
      selectIndex
    };
  },
});
</script>
<style>


</style>